﻿using BasicNginx.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

namespace BasicNginx.Controllers
{
    /// <summary>
    /// 商品控制器
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class ProductController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<ProductController> _logger;
            private readonly IMemoryCache _memoryCache;

        public ProductController(ILogger<ProductController> logger,
                                IMemoryCache memoryCache)
        {  
            _logger = logger;
            _memoryCache = memoryCache;
            
        }

        /// <summary>
        /// 查询商品xxxxss
        /// </summary>
        /// <param name="productDto"></param>
        /// <returns></returns>
        [HttpGet]
        public Product GetProduct()
        {
            // 1、查询缓存
            Product product = _memoryCache.Get<Product>("product");
            if (product != null)
            {
                Console.WriteLine("缓存中查询商品");
                return product;
            }
            // 2、查询数据库数据
            Console.WriteLine("查询商品");
            Product product1 = new Product();
            _memoryCache.Set<Product>("product", product1);
            return product1;
        }
    }
}
